package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import com.ruoyi.web.controller.tool.CommonWidth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Mynews;
import com.ruoyi.system.service.IMynewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 每日新闻联播Controller
 * 
 * @author ruoyi
 * @date 2023-02-01
 */
@RestController
@RequestMapping("/system/mynews")
public class MynewsController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(MynewsController.class);
    @Autowired
    private IMynewsService mynewsService;

    /**
     * 查询每日新闻联播列表
     */
    @PreAuthorize("@ss.hasPermi('system:mynews:list')")
    @GetMapping("/list")
    public TableDataInfo list(Mynews mynews)
    {
        startPage();
        List<Mynews> list = mynewsService.selectMynewsList(mynews);
        return getDataTable(list);
    }

    /**
     * 导出每日新闻联播列表
     */
    @PreAuthorize("@ss.hasPermi('system:mynews:export')")
    @Log(title = "每日新闻联播", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Mynews mynews)
    {
        List<Mynews> list = mynewsService.selectMynewsList(mynews);
        ExcelUtil<Mynews> util = new ExcelUtil<Mynews>(Mynews.class);
        util.exportExcel(response, list, "每日新闻联播数据");
    }

    /**
     * 获取每日新闻联播详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mynews:query')")
//    @GetMapping(value = "/{id}")
    @GetMapping(value = "/query")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        log.info("获取新闻联播信息:"+user.getUserName());
        AjaxResult ajax = AjaxResult.success();
        Mynews mynews = mynewsService.selectMynewsByMaxId();
        int zimujiange_num = 46;
        if (mynews.getComment().length()>zimujiange_num) {
            String tmp = processNews(mynews,"zhkuohao");
            log.info(String.valueOf(tmp.length()));
            System.out.println(tmp);
            mynews.setComment(CommonWidth.getStringW(tmp,zimujiange_num));
            log.info(mynews.getComment());
        }
        return AjaxResult.success(mynews);
    }



    /*
    yanse
    间隔字符  例如：*
     */
    private String processNews(Mynews mynews,String jg) {
        String tmp = mynews.getComment();
        tmp = tmp.replaceAll("\r\n","");
        tmp = tmp.replaceAll("\n","");
        tmp = tmp.replaceAll("完整版","");
        tmp = tmp.replaceAll("《新闻联播》","");
        tmp = tmp.replaceAll("[0-9]{2}:[0-9]{2}:[0-9]{2}","");
        int a = 1;
        while (true) {
            if ("yanse".equals(jg)) {
                //"#ff0000 red#,#00ff00 green#,#0000ff blue#"   #0000ff 需要特殊处理
                tmp = tmp.replaceFirst("\\[视频\\]", " #00ff00 " + a++ + ":#");
            }
            else if ("zhkuohao".equals(jg)) {
                //"#ff0000 red#,#00ff00 green#,#0000ff blue#"   #0000ff 需要特殊处理
                tmp = tmp.replaceFirst("\\[视频\\]", " 【" + a++ + "】");
            }
            else {
                tmp = tmp.replaceFirst("\\[视频\\]", " " + jg + " " + a++ + ":");
            }
            if (tmp.indexOf("[视频]")<0) {
                break;
            }
        }
        System.out.println("新闻联播长度:"+tmp.length());
        return tmp;
    }

    /**
     * 新增每日新闻联播
     */
    @PreAuthorize("@ss.hasPermi('system:mynews:add')")
    @Log(title = "每日新闻联播", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Mynews mynews)
    {
        return toAjax(mynewsService.insertMynews(mynews));
    }

    /**
     * 修改每日新闻联播
     */
    @PreAuthorize("@ss.hasPermi('system:mynews:edit')")
    @Log(title = "每日新闻联播", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Mynews mynews)
    {
        return toAjax(mynewsService.updateMynews(mynews));
    }

    /**
     * 删除每日新闻联播
     */
    @PreAuthorize("@ss.hasPermi('system:mynews:remove')")
    @Log(title = "每日新闻联播", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mynewsService.deleteMynewsByIds(ids));
    }


}
